package edu.hncj.seckill.service.impl;

import edu.hncj.seckill.dao.ProductMapper;
import edu.hncj.seckill.domain.Product;
import edu.hncj.seckill.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author anoraker
 * @Date 2021/5/3 8:26 下午
 * @Version 1.0
 */
@Service
public class ProductServiceImpl implements ProductService {



    @Autowired
    ProductMapper productMapper;

    /**
     * 获取当前未结束的所有课抢购商品,即当前时间大于结束时间
     *
     * @return 商品集合
     */
    @Override
    public List<Product> getProductPage() {
        List<Product> list = productMapper.getAll();
        List<Product> newList = new LinkedList<>();
        for (Product product : list) {
            newList.add(changePicPath(product));
        }
        return newList;
    }

    @Override
    public List<Product> getNowAll() {
        List<Product> list = productMapper.getNowAll();
        List<Product> newList = new LinkedList<>();
        for (Product product : list) {
            newList.add(changePicPath(product));
        }
        return newList;
    }

    @Override
    public List<Product> getAllAfter() {
        List<Product> list = productMapper.getAllAfter();
        List<Product> newList = new LinkedList<>();
        for (Product product : list) {
            newList.add(changePicPath(product));
        }
        return newList;
    }

    /**
     * 通过id查询商品对象
     *
     * @return 商品对象
     */
    @Override
    public Product getProductById(String id) {
        return changePicPath(productMapper.selectById(id));
    }


    @Value("${images.prex}")
    private String prexPath;

    @Value("${images.old}")
    private String oldPath;


    /**
     *  转换商品的图片地址(管理系统的商城系统分开了,所以需要对图片进行相应的处理)
     * @param product 商品对象
     * @return
     */
    private Product changePicPath(Product product){
        if (product.getImg() == null || "".equals(product.getImg())){
            product.setImg("./images/defalut.jpeg");
            return product;
        }
        String nowPath = product.getImg();
        product.setImg(nowPath.replace(oldPath,prexPath));
        return product;
    }

}
